﻿using UnityEngine;
using System.Collections;

public class FadeOutScript : MonoBehaviour 
{
    public void FadeOut(float time)
    {
        StartCoroutine("Fade", time);
    }

    private IEnumerator Fade(float time)
    {
        float t = 0;
        Color color = GetComponent<GUIText>().color;

        while (t / time < 1)
        {
            t += Time.deltaTime;
            float alpha = 1 - (t / time);
            GetComponent<GUIText>().color = new Color(color.r, color.g, color.b, alpha);
            yield return null;
        }

        ObjectPool.instance.Destroy(this.gameObject);
    }
}
